{% extends "templates/main_template.html.j2" %}

{% import "macros/file_input.html.j2" as FILE_INPUT %}
{% import "macros/tab_switcher.html.j2" as TAB_SWITCHER %}
{% import "macros/switch.html.j2" as SWITCH %}
{% import "macros/markdown_textarea.html.j2" as MARKDOWN_TEXTAREA %}
{% import "macros/select_options.html.j2" as SELECT_OPTIONS %}

{% set active_menu = 'issues' %}
{% set issue_kind = project_manager.issue_kind(new_issue.issue_kind_id) %}
{% set workflow = project_manager.workflow(issue_kind.workflow_id) %}
{% set value_types = project_manager.value_types() %}

{% block title -%}
  Создание: «{{ workflow.caption|e }} — {{ issue_kind.caption|e }}» · Farado
{%- endblock %}

{#----------------------------------------------------------------------------#}
{% block styles %}
  {{ FILE_INPUT.STYLES() }}
  {{ SELECT_OPTIONS.STYLES() }}
  {{ MARKDOWN_TEXTAREA.STYLES() }}
  <link href="/static/css/page-toolbar.css" rel="stylesheet" type="text/css" />
{% endblock %}


{#----------------------------------------------------------------------------#}
{% block breadcrumb %}
  <ol class="breadcrumb">
    <li class="breadcrumb-item active" aria-current="page">
      Создание нового запроса: «{{ workflow.caption|e }} — {{ issue_kind.caption|e }}»
    </li>
  </ol>
{% endblock %}


{#----------------------------------------------------------------------------#}
{% block content %}

  <div class="row placeholders">
    <form class="row g-3" action="/issues/issue" method="post" enctype="multipart/form-data">
      <input type="hidden" name="issue_kind_id" value="{{ new_issue.issue_kind_id }}">
      <input type="hidden" name="issue_state_id" value="{{ new_issue.state_id }}">

      {# -- Проект -- #}
        <div class="col-md-6">
          <label for="issue_project_id" class="form-label">Проект</label>
          {{ SELECT_OPTIONS.INPUT_SINGLE(
              data_source = 'projects',
              name = 'issue_project_id',
              project_manager = project_manager,
              selected_id = new_issue.project_id)
          }}
        </div>
      {# -- /Проект -- #}

      {# -- Версия -- #}
        <div class="col-md-3">
          <label for="issue_version_id" class="form-label">Версия</label>
          {{ SELECT_OPTIONS.INPUT_SINGLE(
              data_source = 'projects_versions',
              name = 'issue_version_id',
              project_manager = project_manager,
              selected_id = new_issue.version_id)
          }}
        </div>
      {# -- /Версия -- #}

      {# -- Родительский запрос -- #}
        <div class="col-md-3">
          <label for="inputCaption" class="form-label">Идентификатор родителя</label>
          <input
              type="number"
              name="issue_parent_id"
              class="form-control"
              id="inputCaption"
              value="{{ new_issue.parent_id }}"
              {% if new_issue.parent_id %}readonly{% endif %}>
        </div>

        {% set parent_issue = project_manager.issue(new_issue.parent_id) %}
        {% if parent_issue %}
          <div class="col-12">
            <label
                for="parent_issue_caption"
                class="form-label">Наименование родительского запроса</label>
            <input
                type="text"
                class="form-control"
                id="parent_issue_caption"
                value="{{ parent_issue.caption|e }}"
                readonly
                >
          </div>
        {% endif %}
      {# -- /Родительский запрос -- #}

      {# -- Наименование -- #}
        <div class="col-12">
          <label for="issue_caption" class="form-label">Наименование</label>
          <input
              type="text"
              name="issue_caption"
              class="form-control"
              id="issue_caption"
              value="" required>
        </div>
      {# -- /Наименование -- #}

      {# -- Описание -- #}
        <div class="col-12">
          <label for="contentTab" class="form-label">Описание</label>
          {{ MARKDOWN_TEXTAREA.INPUT(
              tab_id = 'contentTab',
              result_id = 'issue_content_result',
              field_name = 'issue_content',
              field_value = project_manager.safe_value(
                  issue_kind.default_content_value()
              ) )
          }}
          <script>
            document_ready_functions.push(function() {
              {{ TAB_SWITCHER.DOCUMENT_READY_SCRIPT('contentTab') }}
              markdownToHtml(
                'issue_content',
                'issue_content_result'
              );
            });
          </script>
        </div>
      {# -- /Описание -- #}

      <hr/>

      {# -- Issue.fields -- #}
        {% for field in new_issue.fields %}
          <div class="col-md-6">
            {% set field_kind = project_manager.field_kind(field.field_kind_id) %}
            {% if field_kind %}
              <div class="form-group">
                <label
                    for="inputField_{{ field_kind.id }}"
                    class="col-sm-12 col-form-label">{{ field_kind.caption|e }}</label>

                <div class="col-sm-10">
                  {% if value_types.string == field_kind.value_type %}
                    {# Строка #}
                    <input
                        type="text"
                        name="field_kind_{{ field_kind.id }}"
                        class="form-control"
                        id="inputField_{{ field_kind.id }}">

                  {%- elif value_types.markdown_text == field_kind.value_type %}
                    {# Текст markdown #}
                    {{ MARKDOWN_TEXTAREA.INPUT(
                        tab_id = 'inputField_' + field_kind.id | string + '',
                        result_id = 'field_kind_' + field_kind.id | string + '_result',
                        field_name = 'field_kind_' + field_kind.id | string,
                        field_value = '' )
                    }}
                    <script>
                      document_ready_functions.push(function() {
                        {{ TAB_SWITCHER.DOCUMENT_READY_SCRIPT(
                              'inputField_' + field_kind.id | string )
                        }};
                      });
                    </script>

                  {% elif value_types.integer == field_kind.value_type %}
                    {# Целое число #}
                    <input
                        type="number"
                        name="field_kind_{{ field_kind.id }}"
                        class="form-control"
                        id="inputField_{{ field_kind.id }}">

                  {% elif value_types.float == field_kind.value_type %}
                    {# Дробное число #}
                    <input
                        type="number"
                        step="any"
                        name="field_kind_{{ field_kind.id }}"
                        class="form-control"
                        id="inputField_{{ field_kind.id }}">

                  {% elif value_types.bool == field_kind.value_type %}
                    {# Флаг #}
                    <div class="form-check form-switch">
                      {{ SWITCH.INPUT(
                          id = 'field_kind_' + field_kind.id|string,
                          name = 'field_kind_' + field_kind.id|string )
                      }}
                    </div>

                  {% elif value_types.date_time == field_kind.value_type %}
                    {# Дата/время #}
                    <input
                        type="datetime-local"
                        name="field_kind_{{ field_kind.id }}"
                        class="form-control"
                        id="inputField_{{ field_kind.id }}">

                  {% elif value_types.uri == field_kind.value_type %}
                    {# Ссылка #}
                    <input
                        type="url"
                        name="field_kind_{{ field_kind.id }}"
                        class="form-control"
                        id="inputField_{{ field_kind.id }}">

                  {% elif value_types.select == field_kind.value_type %}
                    {# Выпадающий список (единичный) #}
                    {{ SELECT_OPTIONS.INPUT_SINGLE(
                        data_source = 'field_kind_select',
                        name = 'field_kind_' + field_kind.id|string,
                        project_manager = project_manager,
                        data_source_id = field_kind.id)
                    }}

                  {% elif value_types.select_multi == field_kind.value_type %}
                    {# Выпадающий список (множественный) #}
                    {{ SELECT_OPTIONS.INPUT_MULTY(
                        data_source = 'field_kind_select',
                        name = 'field_kind_' + field_kind.id|string,
                        project_manager = project_manager,
                        data_source_id = field_kind.id)
                    }}

                  {% elif value_types.issue_id == field_kind.value_type %}
                    {# Запрос (issue) #}
                    {# TODO: Решить вопрос поиска необходимого запроса через ui #}
                    <input
                        type="number"
                        name="field_kind_{{ field_kind.id }}"
                        class="form-control"
                        id="inputField_{{ field_kind.id }}">

                  {% elif value_types.user_id == field_kind.value_type %}
                    {# Пользователь #}
                    {{ SELECT_OPTIONS.INPUT_SINGLE(
                        data_source = 'users',
                        name = 'field_kind_' + field_kind.id|string,
                        project_manager = project_manager,
                        data_source_id = user.id)
                    }}

                  {% elif value_types.project_id == field_kind.value_type %}
                    {# Проект #}
                    {{ SELECT_OPTIONS.INPUT_SINGLE(
                        data_source = 'projects',
                        name = 'field_kind_' + field_kind.id|string,
                        project_manager = project_manager)
                    }}

                  {% elif value_types.version_id == field_kind.value_type %}
                    {# Версия #}
                    {{ SELECT_OPTIONS.INPUT_SINGLE(
                        data_source = 'projects_versions',
                        name = 'field_kind_' + field_kind.id|string,
                        project_manager = project_manager)
                    }}

                  {% elif value_types.users_ids == field_kind.value_type %}
                    {# Перечень пользователей #}
                    {{ SELECT_OPTIONS.INPUT_MULTY(
                        data_source = 'users',
                        name = 'field_kind_' + field_kind.id|string,
                        project_manager = project_manager,
                        data_source_id = user.id)
                    }}

                  {% elif value_types.projects_ids == field_kind.value_type %}
                    {# Перечень проектов #}
                    {{ SELECT_OPTIONS.INPUT_MULTY(
                        data_source = 'projects',
                        name = 'field_kind_' + field_kind.id|string,
                        project_manager = project_manager)
                    }}

                  {% elif value_types.versions_ids == field_kind.value_type %}
                    {# Перечень версий #}
                    {{ SELECT_OPTIONS.INPUT_MULTY(
                        data_source = 'projects_versions',
                        name = 'field_kind_' + field_kind.id|string,
                        project_manager = project_manager)
                    }}
                  {% endif %}
                </div>
              </div>
            {% endif %}
          </div>
        {% endfor %}
      {# -- /Issue.fields -- #}

      <hr/>

      {# -- Issue.files -- #}
        <div class="col-12">
          {{ FILE_INPUT.INPUT('issue_files_editor', 0) }}
          <script>
            document_ready_functions.push(function() {
              $("#issue_files_editor").fileinput({
                previewFileType: 'any',
                language: 'ru',
                overwriteInitial: false,
                showUpload: false,
              });
            });
          </script>
        </div>
      {# -- /Issue.files -- #}

    <div class="col-md-12">
      <div class="button-container">
        <button type="submit" id="saveButton" class="btn btn-success">
          <i class="bi bi-save"></i> Сохранить
        </button>
      </div>
    </div>

    </form>
  </div>
{% endblock %}



{#----------------------------------------------------------------------------#}
{% block scripts %}
  {{ FILE_INPUT.SCRIPTS() }}
  {{ MARKDOWN_TEXTAREA.SCRIPTS() }}
  {{ SELECT_OPTIONS.SCRIPTS() }}
{% endblock %}
